#!/usr/bin/python
import pygame
from data import *

class Dialog(object):
    """A Dialog has an 'Ok'-button as a standard and can have one extra. 
    Depending on the name of the extra button the function behind it should be written in de eventloop in main()."""
    
    def __init__(self, screen, title, message, extra_button_text = None, scene = None):
        """Initialization of Inputbox.
    Creates the necessary global parameters, including several rectangles which will be used to draw the dialog.
    Parameters: display screen
                string title - title of the dialog
                string message - list of strings (lines)
                string extra_button_text - title of the possible extra button
                Scene scene - instance of Scene()"""
        print 'Initializing Dialog'
        self.screen = screen
        self.font = pygame.font.Font(None, 32)
        self.title = self.font.render(title, 1, (255, 255, 255))
        self.message = message
        self.extra_button_text = extra_button_text
        self.scene = scene
        self.keepGoing = True
        self.size = self.width, self.height = 500, 275
        self.user_id = ''
        
        self.surface = pygame.Rect(((self.screen.get_width() / 2 - self.width / 2), (self.screen.get_height() / 2 - self.height / 2)), (self.size))
        self.surface_contour = pygame.Rect(self.surface.x - 1, self.surface.y - 1, self.surface.width + 2, self.surface.height + 2)
        self.bar = pygame.Rect(self.surface.x, self.surface.y, self.width, 35)
        self.bar_contour = pygame.Rect(self.surface.x, self.surface.y, self.width, self.bar.height)
        
        self.button_ren = self.font.render('OK', 1, (255, 255, 255))
        self.button = pygame.Rect(0, 0, self.button_ren.get_width() + 20, self.button_ren.get_height() + 20)
        x, y = self.surface.bottomright
        self.button.bottomright = x - 10, y - 10
        self.button_contour = pygame.Rect(self.button.x - 1, self.button.y - 1, self.button.width + 2, self.button.height + 2)
    
    def draw_extra_button(self):
        """Draws the extra button."""
        self.extra_button_ren = self.font.render(self.extra_button_text, 1, (255, 255, 255))
        self.extra_button = pygame.Rect(0, 0, self.extra_button_ren.get_width() + 20, self.extra_button_ren.get_height() + 20)
        x, y = self.surface.bottomleft
        self.extra_button.bottomleft = x + 10, y - 10
        self.extra_button_contour = pygame.Rect(self.extra_button.x -1, self.extra_button.y - 1, self.extra_button.width + 2, self.extra_button.height + 2)
        
        if mouse_collision(self.extra_button):
            pygame.draw.rect(self.screen, (45, 45, 45), self.extra_button)
        else:
            pygame.draw.rect(self.screen, (40, 77, 171), self.extra_button)
        pygame.draw.rect(self.screen, (150, 150, 150), self.extra_button_contour, 1)
        self.screen.blit(self.extra_button_ren, \
                            (self.extra_button.x + (self.extra_button.width / 2 - self.extra_button_ren.get_width() / 2), \
                            (self.extra_button.y + (self.extra_button.height / 2 - self.extra_button_ren.get_height() / 2))))

    def set_user_id(self, user_id):
        """Sets the global user_id parameter.
        Parameters: int user_id - user identification code"""
        self.user_id = user_id
        
    def main(self):
        """Main loop of the dialog, this will overrule the main loop of scene untill this one is ended.
        Draws all the necessary rectangles, strings and images for the dialog.
        Checks of key events.
        Quit and escape exit the dialog. Mousebuttonup is also tracked for both the standard button and the extra button.
        If a new extra button is added this should also be added in the event loop. """
        while self.keepGoing:
            pygame.draw.rect(self.screen, (255, 255, 255), self.surface)
            pygame.draw.rect(self.screen, (150, 150, 150), self.surface_contour, 2)
            pygame.draw.rect(self.screen, (40, 77,  171), self.bar)
            pygame.draw.rect(self.screen, (0, 0, 0), self.bar_contour, 1)
            self.screen.blit(self.title, (self.bar.x + 5, self.bar_contour.y + (self.bar.height / 2 - self.title.get_height() / 2)))
            y = 1
            for line in self.message:
                ren = self.font.render(line, 1, (45, 45, 45))
                y_point = (y * ren.get_height()) + (y * 20)
                self.screen.blit(ren, (self.surface.x + 15, (y_point + 175)))
                y += 1
            if mouse_collision(self.button):
                pygame.draw.rect(self.screen, (45, 45, 45), self.button)
            else:
                pygame.draw.rect(self.screen, (40, 77, 171), self.button)
            pygame.draw.rect(self.screen, (150, 150, 150), self.button_contour, 1)
            self.screen.blit(self.button_ren, \
                            (self.button.x + (self.button.width / 2 - self.button_ren.get_width() / 2), \
                            (self.button.y + (self.button.height / 2 - self.button_ren.get_height() / 2))))
            if self.extra_button_text:
                self.draw_extra_button()
            pygame.display.flip()
            
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        self.keepGoing = False
                elif event.type == pygame.KEYUP:
                    if event.key == pygame.K_ESCAPE:
                        self.keepGoing = False
                elif event.type == pygame.MOUSEBUTTONUP:
                    if mouse_collision(self.button):
                        self.keepGoing = False
                        try:
                            self.scene.box.current_string = []
                        except:
                            pass
                    elif mouse_collision(self.extra_button):
                        if self.extra_button_text == 'Shoot Picture':
                            self.keepGoing = False
                            self.scene.box.current_string = ['%s' % self.user_id]
                            self.scene.take_picture_window()
                            
